﻿using System;
using UnityEngine;

namespace Z1029.NoteEditor
{
    public class NotesManager : MonoBehaviour
    {
        public Note note;

        public static NotesManager instance;

        private void Start()
        {
            instance = this;
        }

        public Note InstantiateNote(NoteGlobalView.Note note,JudgementLine judgeline)
        {
            GameObject gameObject = Instantiate(this.note.gameObject);
            Note component = gameObject.GetComponent<Note>();
            Note.NoteData data = new Note.NoteData
            {
                speed = note.noteData.speed,
                beatHoldTime = note.noteData.beatHoldTime,
                beatTime = note.noteData.beatTime,
                positionX = note.noteData.positionX,
                type = (NoteType)note.noteData.type
            };
            component.noteData = data;
            component.targetNote = note;
            component.direction = (Note.Direction)note.direction;
            gameObject.transform.parent = judgeline.transform;

            gameObject.name = "Note - " + data.time;

            component.Awake();
            component.Start();

            return component;
        }

        public Note InstantiateNote(NoteType type, Note.NoteData data, JudgementLine judgeline, Note.Direction dir)
        {
            NoteGlobalView.Note.NoteData dat = new NoteGlobalView.Note.NoteData();
            dat.type = (NoteGlobalView.NoteType)type;
            dat.positionX = data.positionX;
            dat.speed = data.speed;
            dat.beatTime = data.beatTime;
            dat.beatHoldTime = data.beatHoldTime;
            return InstantiateNote(
                            NoteGlobalView.LevelController.instance.notesManager.InstantiateNote(
                                (NoteGlobalView.NoteType)type,
                                dat,
                                NoteGlobalView.JudgementLine.selectedJudgeline,
                                (NoteGlobalView.Note.Direction)dir),
                            LevelController.instance.judgelines[0]
                            );
        }
    }
}